package com.geese.module.admin.controller;

import com.alibaba.druid.util.StringUtils;
import com.geese.common.BaseController;
import com.geese.common.util.TextValidator;
import com.geese.common.vo.Page;
import com.geese.exception.BusinessException;
import com.geese.module.admin.dto.AdminDTO;
import com.geese.module.admin.service.IAdminService;
import com.geese.module.admin.vo.AdminVO;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 管理员控制器
 * Created by huweiwei on 2017/3/29.
 */
@RestController
@RequestMapping("/admin/manager")
public class AdminController extends BaseController{


    @Autowired
    private IAdminService adminService;

    @GetMapping("/list")
    public String list(@RequestParam String token, AdminVO adminVO) {
        Page page = getPageInfo(adminVO);
        Map<String, Object> result = adminService.selectList(page, adminVO);
        return response(result);
    }

    @PostMapping("/add")
    public String add(@RequestParam String token, @RequestBody AdminDTO adminDTO){
        if (StringUtils.isEmpty(adminDTO.getAccount()) || !TextValidator.isMobileExact(adminDTO.getAccount())) {
            throw new BusinessException(120113);
        }
        if (StringUtils.isEmpty(adminDTO.getPassword())) {
            throw new BusinessException(120114);
        }
        if (StringUtils.isEmpty(adminDTO.getDepartment())) {
            throw new BusinessException(120115);
        }
        if (StringUtils.isEmpty(adminDTO.getUsername())) {
            throw new BusinessException(120116);
        }
        if (StringUtils.isEmpty(adminDTO.getRoleId())) {
            throw new BusinessException(120117);
        }
        List<Integer> ids = Lists.transform(Arrays.asList(adminDTO.getRoleId().split(",")), new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                return Integer.valueOf(s);
            }
        });
        adminDTO.setRoleIds(ids);
        if (CollectionUtils.isEmpty(adminDTO.getRoleIds())) {
            throw new BusinessException(120117);
        }
        if (adminDTO.getBlock() == null || (adminDTO.getBlock() != 1 && adminDTO.getBlock() != 0)) {
            throw new BusinessException(120118);
        }
        adminService.addAdmin(adminDTO);
        return response();
    }

    @PostMapping("/delete")
    public String delete(@RequestParam String token, @RequestParam int id) {
        if (id <= 0) {
            throw new BusinessException(120009);
        }
        adminService.delete(id);
        return response();
    }

    @PostMapping("/deleteAll")
    public String delete(@RequestParam String token, @RequestParam List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            throw new BusinessException(120009);
        }
        adminService.deleteBatch(ids);
        return response();
    }

    @PostMapping("/edit")
    public String edit(@RequestParam String token, @RequestBody AdminDTO adminDTO) {
        if (adminDTO.getId() == null || adminDTO.getId() <= 0) {
            throw new BusinessException(120009);
        }
        adminService.updateAdmin(adminDTO);
        return response();
    }

    @GetMapping("/details")
    public String details(@RequestParam String token, @RequestParam int id) {
        if (id <= 0) {
            throw new BusinessException(120009);
        }
        Map<String, Object> data = adminService.getDetails(id);
        return response(data);
    }
}
